package com.cskaoyan.wordmemorize.excel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.ExcelParseException;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import org.springframework.core.io.ResourceLoader;

import java.util.Map;


public abstract class AbstractTopicExcelValidator extends AbstractVocExcelValidator{

    VocMapper vocMapper;
    public void init(ResourceLoader resourceLoader, VocMapper vocMapper) {
        super.init(resourceLoader);
        this.vocMapper = vocMapper;
    }

    @Override
    public void doValidate(Integer rowNum, Map<Integer, String> data) {

        // 校验单词是否存在
        String[] topicValidateWords = getTopicValidateWords(data);

        for (int i = 0; i < topicValidateWords.length; i++) {
            LambdaQueryWrapper<VocDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VocDO::getContent, topicValidateWords[i]);

            Long count = vocMapper.selectCount(queryWrapper);
            if (count == 0) {
                throw new ExcelParseException(ResultCodeEnum.VOC_EXCEL_PARSE_ERROR, rowNum, "单词不存在");
            }
        }


    }

    public abstract String[] getTopicValidateWords(Map<Integer, String> data);
}
